package library.router;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;

/**
 * @author Created by qiang.hou on 2018/12/12.
 * @version 1.0
 */
public class Router {

    private Router() {
    }

    private static class RouterHolder {
        private static Router INSTANCE = new Router();
    }

    public static Router getInstance() {
        return RouterHolder.INSTANCE;
    }

    /**
     * init Router
     *
     * @param isDebug     BuildConfig.DEBUG
     * @param application Application
     */
    public void init(boolean isDebug, Application application) {
        if (isDebug) {
            // 这两行必须写在init之前，否则这些配置在init过程中将无效
            // 打印日志
            ARouter.openLog();
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
            //  ARouter.printStackTrace(); // 打印日志的时候打印线程堆栈
        }
        ARouter.init(application); // 尽可能早，推荐在Application中初始化
    }

    /**
     * get fragment by ModuleName.FragmentName
     *
     * @param fragmentPath path
     * @param <T>          FragmentType(app.Fragment / v4.app.Fragment)
     * @return Fragment
     */
    @SuppressWarnings("unchecked")
    public <T> T getFragment(String fragmentPath) {
        if (null == fragmentPath || fragmentPath.equals("")) {
            return null;
        }
        return (T) ARouter.getInstance().build(fragmentPath).navigation();
    }

    /**
     * @param servicePath path
     * @param <T>         ServiceType
     * @return Service
     */
    @SuppressWarnings("unchecked")
    public <T> T getService(String servicePath) {
        if (null == servicePath || servicePath.equals("")) {
            return null;
        }
        return (T) ARouter.getInstance().build(servicePath).navigation();
    }

    /**
     * @param activityPath path
     */
    public void startActivity(String activityPath) {
        if (null == activityPath || activityPath.equals("")) {
            return;
        }
        ARouter.getInstance().build(activityPath).navigation();
    }

    /**
     * @param activity     from activity
     * @param activityPath path
     * @param requestCode  requestCode
     */
    public void startActivityForResult(Activity activity, String activityPath, int requestCode) {
        if (null == activity || null == activityPath || activityPath.equals("")) {
            return;
        }
        ARouter.getInstance().build(activityPath).navigation(activity, requestCode);
    }

    /**
     * @param activityPath path
     * @param bundle       Bundle
     */
    public void startActivity(String activityPath, Bundle bundle) {
        if (null == activityPath || activityPath.equals("")) {
            return;
        }
        ARouter.getInstance().build(activityPath).with(bundle).navigation();
    }

    /**
     * @param activity     from activity
     * @param activityPath path
     * @param bundle       Bundle
     * @param requestCode  requestCode
     */
    public void startActivityForResult(Activity activity, String activityPath, Bundle bundle, int requestCode) {
        if (null == activity || null == activityPath || activityPath.equals("")) {
            return;
        }
        ARouter.getInstance().build(activityPath).with(bundle).navigation(activity, requestCode);
    }

    /**
     * @param activityPath path
     * @param bundle       Bundle
     * @param enterAnim    enterAnim
     * @param exitAnim     exitAnim
     */
    public void startActivity(String activityPath, Bundle bundle, int enterAnim, int exitAnim) {
        if (null == activityPath || activityPath.equals("")) {
            return;
        }
        Postcard postcard = ARouter.getInstance().build(activityPath).with(bundle);
        if (enterAnim > 0 && exitAnim > 0) {
            postcard.withTransition(enterAnim, exitAnim);
        }
        postcard.navigation();
    }

    /**
     * @param activityPath path
     * @param bundle       Bundle
     * @param enterAnim    enterAnim
     * @param exitAnim     exitAnim
     * @param activity     from activity
     * @param requestCode  requestCode
     */
    public void startActivityForResult(Activity activity, String activityPath, Bundle bundle, int requestCode, int enterAnim, int exitAnim) {
        if (null == activity || null == activityPath || activityPath.equals("")) {
            return;
        }
        Postcard postcard = ARouter.getInstance().build(activityPath).with(bundle);
        if (enterAnim > 0 && exitAnim > 0) {
            postcard.withTransition(enterAnim, exitAnim);
        }
        postcard.navigation(activity, requestCode);
    }

    /**
     * @param activityPath path
     * @return Postcard
     */
    public Postcard buildActivity(String activityPath) {
        if (null == activityPath || activityPath.equals("")) {
            return null;
        }
        return ARouter.getInstance().build(activityPath);
    }
}
